package com.example.featuresdk.core

import android.util.Log

class NavigationMediator: INavigationMediator {

    val features: MutableMap<String,AbstractFeatureSDK> = mutableMapOf()

    lateinit var homeFeatureKey: String

//    val features: MutableList<AbstractFeatureSDK> = mutableListOf()

    override fun back() {
        TODO("Not yet implemented")

    }

    override fun currentFeature(): AbstractFeatureSDK {
        TODO("Not yet implemented")
    }

    override fun targetFeature(): AbstractFeatureSDK {
        TODO("Not yet implemented")
    }

    override fun feature(featureKey: String): AbstractFeatureSDK {
//        return features.lastOrNull { it -> it.featureKey() == featureKey }
        return features[featureKey]!!
    }

    override fun register(featureSDK: AbstractFeatureSDK) {

//        if(!features.any { it -> it.featureKey() == featureSDK.featureKey() }){
//            features.add(featureSDK)
//            Log.i("$this","registered: ${featureSDK.featureKey()}")
//        }

        if(!features.contains(featureSDK.featureKey())){
            features[featureSDK.featureKey()] = featureSDK
        }
        if(features.size == 1){
            homeFeatureKey = featureSDK.featureKey()
            Log.i("NavigationMediator","homeFeatureKey = $homeFeatureKey")
        }
    }

    fun features(): List<AbstractFeatureSDK> {
        return features.values.toList()
    }
}